package com.ljl.mvvmdemo.util

import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import com.ljl.mvvmdemo.db.entity.Login
import com.ljl.mvvmdemo.db.entity.PosMachine
import com.ljl.mvvmdemo.db.getAppDataBase
import com.ljl.mvvmdemo.getApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import java.lang.reflect.Method
import kotlin.coroutines.CoroutineContext

/**
 * 全局数据获取帮助类
 * created by luojialun on 2024/10/23
 */
class GlobalDataHelper : CoroutineScope {

    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Default

    companion object {
        @Volatile
        private var instance: GlobalDataHelper? = null

        fun getInstance(): GlobalDataHelper {
            return instance ?: synchronized(this) {
                instance ?: GlobalDataHelper().also { instance = it }
            }
        }
    }

    private var login: Login? = null //登录信息

    fun getLogin(): Login {
        return if (null == login) {
            runBlocking {
                login = getAppDataBase().getLoginDao().getLogin()
                login ?: Login()
            }
        } else {
            login!!
        }
    }

    private var terminalId: String = ""

    fun getTerminalId(): String {
        return if (TextUtils.isEmpty(terminalId)) {
            getSerialNumber()
        } else {
            terminalId
        }
    }

    private fun getSerialNumber(): String {
        var serial = ""
        try {
            serial = when {
                Build.VERSION.SDK_INT >= 30 -> {
                    Settings.System.getString(getApplication().contentResolver, Settings.Secure.ANDROID_ID)
                }
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.P -> { //9.0+
                    Build.getSerial()
                }
                Build.VERSION.SDK_INT > Build.VERSION_CODES.N -> { //8.0+
                    Build.SERIAL
                }
                else -> { //8.0-
                    val c = Class.forName("android.os.SystemProperties")
                    val get: Method = c.getMethod("get", String::class.java)
                    get.invoke(c, "ro.serialno") as String
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        if (TextUtils.isEmpty(serial) || "unknown" == serial) {
            serial = Settings.System.getString(getApplication().contentResolver, Settings.Secure.ANDROID_ID)
        }
        if (TextUtils.isEmpty(serial)) {
            serial = "00000000"
        }
        return serial

    }

    private var posMachine: PosMachine? = null

    fun getPosMachine(): PosMachine {
        return if (null == posMachine) {
            runBlocking {
                posMachine = getAppDataBase().getPosMachineDao().getPosMachine(getTerminalId())
                posMachine ?: PosMachine()
            }
        } else {
            posMachine!!
        }
    }

}